package com.buercorp.wangyu.core1.ch12.threads.threadsPool;

import java.util.concurrent.*;

/**
 * 线程池处理 Runnable 任务
 * @author liyiyu
 */
public class ThreadRunnableText {
    public static void main(String[] args) {
        /**
         * 通过 ThreadPoolExecutor创建线程池
         * public ThreadPoolExecutor(int corePoolSize,
         *                               int maximumPoolSize,
         *                               long keepAliveTime,
         *                               TimeUnit unit,
         *                               BlockingQueue<Runnable> workQueue) {
         *         this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
         *              Executors.defaultThreadFactory(), defaultHandler);
         *     }
          */
        ThreadPoolExecutor pool = new ThreadPoolExecutor(3, // 核心
                5,
                6,
                TimeUnit.SECONDS, // 单位时间
                new ArrayBlockingQueue<>(4), // 任务队列
                Executors.defaultThreadFactory(), // 创建线程工厂
                new ThreadPoolExecutor.AbortPolicy() // 任务拒绝策略\

        );

        // 任务对象
        Runnable tagen = new MyRunnable();
        // 把任务对象交给线程池 线程池自动创建一个新的线程 自动处理这个任务 自动执行
        pool.execute(tagen); // 核心线程
        pool.execute(tagen); // 核心线程
        pool.execute(tagen); // 核心线程

        pool.execute(tagen); // 任务队列
        pool.execute(tagen); // 任务队列
        pool.execute(tagen); // 任务队列
        pool.execute(tagen); // 任务队列

        pool.execute(tagen); // 临时
        pool.execute(tagen); // 临时

        /*pool.submit(new MyRunnable());
        pool.submit(new MyRunnable());
        pool.submit(new MyRunnable());

        pool.submit(new MyRunnable());
        pool.submit(new MyRunnable());
        pool.submit(new MyRunnable());
        pool.submit(new MyRunnable());

        pool.submit(new MyRunnable());
        pool.submit(new MyRunnable());*/

        pool.shutdown(); // 等着线程池全部执行完 关闭线程池ThreadRunnableText

    }
}
